﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Groupbox;

namespace OntoWPFSilverlight.Widget
{
    public class CanvasForm : Canvas, Interfaces.IWidgetContainer
    {
        protected System.Collections.Generic.List<Interfaces.IWidget> widgets;
        protected System.Collections.Generic.List<Interfaces.IWidgetViewer> viewerWidgets;
        TextBox label;
        Canvas can;
        
        
        public CanvasForm() 
        {
            
            Build();
        }

        private void Build()
        {
            widgets = new System.Collections.Generic.List<Interfaces.IWidget>();
            viewerWidgets = new System.Collections.Generic.List<Interfaces.IWidgetViewer>();
            Border br = new Border();
            br.Style = (Style)Application.Current.Resources["GlassBorderStyle"];
            StackPanel panel = new StackPanel();
            label = new TextBox();
            label.IsReadOnly = true;
            can = new Canvas();
            panel.Children.Add(label);
            panel.Children.Add(can);
            br.Child = panel;
            //this.HorizontalAlignment = HorizontalAlignment.Center;
            //this.VerticalAlignment = VerticalAlignment.Center;
            this.Children.Add(br);
       }
        
        
        #region IWidgetContainer Membri di

        public System.Collections.Generic.List<Interfaces.IWidget> Widgets
        {
            get
            {
                return widgets;
            }
            set
            {
                widgets = value;
            }
        }

        public System.Collections.Generic.List<Interfaces.IWidgetViewer> ViewerWidgets
        {
            get
            {
                return viewerWidgets;
            }
            set
            {
                viewerWidgets = value;

            }
        }



        protected Xml.Ontology ontology;
        public Xml.Ontology Ontology
        {
            get
            {
                return ontology;
            }
            set
            {
                ontology = value;
            } 
        }


        protected Xml.WidgetSize widgetSize;
        public Xml.WidgetSize WidgetSize
        {
            get
            {
                return widgetSize;
            }
            set
            {
                try
                {
                    widgetSize = value;
                    //System.Windows.Browser.HtmlPage.Window.Alert("slider setter" + widgetSize.Height);
                    can.Height = widgetSize.Height;
                    can.Width = widgetSize.Width;
                    
                    //this.HorizontalAlignment = HorizontalAlignment.Center;
                    //this.VerticalAlignment = VerticalAlignment.Center;
                }
                catch (Exception)
                {
                    //TODO: 
                }
            }
        }

        protected Xml.WidgetFont widgetFont;
        public Xml.WidgetFont WidgetFont
        {
            get
            {
                return widgetFont;
            }
            set
            {
                try
                {
                    widgetFont = value;
                    label.TextAlignment = TextAlignment.Center;
                    label.FontStyle = FontStyles.Normal;
                    label.FontFamily = new FontFamily(this.widgetFont.Name);
                    try
                    {
                        label.FontSize = Double.Parse(this.widgetFont.Size);
                    }
                    catch (Exception)
                    {
                        
                    }
                }
                catch (Exception)
                {
                    //TODO: 
                }
            }
        }


        public Xml.Appearance appearance;
        public Xml.Appearance Appearance
        {
            get
            {
                return appearance;
            }
            set
            {
                appearance = value;
                try
                {
                    byte r, g, b;
                    byte.TryParse(appearance.BackColor.Split(',')[0], out r);
                    byte.TryParse(appearance.BackColor.Split(',')[1], out g);
                    byte.TryParse(appearance.BackColor.Split(',')[2], out b);
                    can.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));
                }
                catch (Exception)
                {
                    //TODO: 
                }
            }
        }

        public string Text
        {
            get
            {
                return this.label.Text;
            }
            set
            {
                this.label.Text = value;
            }
        }

        public void update() {
            foreach (OntoWPFSilverlight.Interfaces.IWidget _widget in widgets)
            {

                try
                {
                    
                    try
                    {
                        can.Children.Add((Control)_widget);
                    }
                    catch (Exception)
                    {
                        can.Children.Add((Panel)_widget);
                    }
                    if (typeof(Interfaces.IWidgetWithItems).IsAssignableFrom(_widget.GetType()))
                        ((Interfaces.IWidgetWithItems)_widget).update();
                }
                catch (Exception e)
                {
                    System.Windows.Browser.HtmlPage.Window.Alert("Update ex" + e.StackTrace);
                }

            }
            foreach (OntoWPFSilverlight.Interfaces.IWidgetViewer _widget in viewerWidgets)
            {
                
                can.Children.Add((Panel)_widget);
            }
        }

        
        #endregion
    }
}
